package com.hspedu.hspliving.commodity.web;

import com.hspedu.hspliving.commodity.entity.CategoryEntity;
import com.hspedu.hspliving.commodity.service.CategoryService;
import com.hspedu.hspliving.commodity.vo.Catalog2Vo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author: xujiabing
 * @date: 2024/8/15 20:37
 * @description web页面控制器
 */
@Slf4j
@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 响应用户请求首页 http://localhost:9090
     * 或 http://localhost:9090/index.html
     * @param model
     * @return
     */
    @GetMapping(value = {"/","/index.html"})
    private String indexPage(Model model) {
        //获取一级分类列表
        List<CategoryEntity> level1Categorys = categoryService.getLevel1Categorys();
        //放入model域中
        model.addAttribute("categories",level1Categorys);

        //默认找的是"classpath\templates\"+"index"+".html"
        return "index";
    }

    /**
     * 返回指定格式2，3级分类
     * @return
     */
    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catalog2Vo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }
}
